import { createRouter, createWebHashHistory } from "vue-router";

const routes = [
  {
    path: "/:pathMatch(.*)*",
    meta: { title: "404页面不存在" },
    component: () => import("../pages/404.vue"),
  },
  {
    path: "/",
    redirect: "/index",
    meta: { title: "layout" },
    component: () => import("../layout/index.vue"),
    children: [
      {
        path: "index",
        meta: { title: "首页", parentPath: "/" },
        component: () => import("../pages/Index/index.vue"),
      },
      {
        path: "preview/:id",
        meta: { title: "博客详情", parentPath: "/" },
        component: () => import("../pages/BlogPreview/index.vue"),
      },
      {
        path: "sort/",
        meta: { title: "归档", parentPath: "/" },
        component: () => import("../pages/Sort/index.vue"),
      },
      {
        path: "sortDetail/:id",
        meta: { title: "归档详情", parentPath: "/" },
        component: () => import("../pages/Sort/SortDetail.vue"),
      },
      {
        path: "photo/",
        meta: { title: "相册", parentPath: "/" },
        component: () => import("../pages/Photo/index.vue"),
      },
      {
        path: "photoDetail/:id",
        meta: { title: "相册详情", parentPath: "/" },
        component: () => import("../pages/Photo/PhotoDetail.vue"),
      },
      {
        path: "message",
        meta: { title: "留言", parentPath: "/" },
        component: () => import("../pages/Message/index.vue"),
      },
      {
        path: "treeHole",
        meta: { title: "树洞", parentPath: "/" },
        component: () => import("../pages/TreeHole/index.vue"),
      },
      {
        path: "tools",
        meta: { title: "工具箱", parentPath: "/" },
        component: () => import("../pages/Tools/index.vue"),
      },
    ],
  },
];
const router = createRouter({
  routes,
  history: createWebHashHistory(),
  scrollBehavior(to, from, savedPosition) {
    return { top: 0 };
  },
});

router.beforeEach((to, from, next) => {
  next();
});
router.afterEach((to, from) => {
  window.document.title = to.meta.title;
});
export default router;
